
/*
 *
 * All Rights Reserved.
 */
package com.model.demo.designmodel.delegate.mvc;

import com.model.demo.designmodel.delegate.mvc.controllers.MemberController;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * DispatcherServlet
 *
 * @author Liao Dongshan, 2019年06月10日
 *
 */
public class DispatcherServlet extends HttpServlet {

    private List<Handle> handleMapping=new ArrayList<>();


    @Override
    public void init() throws ServletException {
        Class<?> memberControllerClass = MemberController.class;
        try {
            handleMapping.add(new Handle().setController(memberControllerClass)
                    .setMethod(memberControllerClass.getMethod("getMemberById",new Class[]{String.class})).setUrl("web/getMemberById.do"));
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //完成调度

        dispatcher(req,resp);

    }

    private void dispatcher(HttpServletRequest req, HttpServletResponse resp) {
        String uri=req.getRequestURI();
        Handle h=null;
        for(Handle handle:handleMapping){
            if(handle.getUrl().equals(uri)){
                h=handle;
                break;
            }
        }
        try {
            Object obj = h.getMethod().invoke(h.getController(), req.getParameter("mid"));
            resp.getWriter().write(obj.toString());
        } catch (IllegalAccessException | IOException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }

//    private void dispatcher(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//        String requestURI = req.getRequestURI();
//        String mid = req.getParameter("mid");
//        if("getMemberById".equals(requestURI)){
//            new MemberController().getMemberById(mid);
//        }else if("getOrderById".equals(requestURI)){
//            new OrderController().getOrderById(mid);
//        }else if("logout".equals(requestURI)){
//            new SystemController().logout();
//        }else{
//            resp.getWriter().write("404 Not Found");
//        }
//    }
    class Handle{
        String url;
        Method method;
        Object controller;

    public String getUrl() {
        return url;
    }

    public Handle setUrl(String url) {
        this.url = url;
        return this;
    }

    public Method getMethod() {
        return method;
    }

    public Handle setMethod(Method method) {
        this.method = method;
        return this;
    }

    public Object getController() {
        return controller;
    }

    public Handle setController(Object controller) {
        this.controller = controller;
        return this;
    }
}



}
